<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="公告标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.noticeTitle"
          clearable
          placeholder="请输入公告标题"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          clearable
          placeholder="请输入操作人员"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="noticeType">
        <el-select v-model="queryParams.noticeType" clearable placeholder="公告类型" size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="small" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:notice:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:notice:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:notice:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="序号" prop="noticeId" width="80"/>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="公告标题"
        prop="noticeTitle"
      />
      <el-table-column
        :formatter="typeFormat"
        align="center"
        label="公告类型"
        prop="noticeType"
        width="100"
      />
      <el-table-column
        :formatter="statusFormat"
        align="center"
        label="状态"
        prop="status"
        width="100"
      />
<!--      <el-table-column align="center" label="创建者" prop="createBy" width="100"/>-->
      <el-table-column align="center" label="创建时间" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:notice:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['system:notice:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="780px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="form.noticeTitle" placeholder="请输入公告标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="24">-->
<!--            <el-form-item label="状态">-->
<!--              <el-radio-group v-model="form.status">-->
<!--                <el-radio-->
<!--                  v-for="dict in statusOptions"-->
<!--                  :key="dict.dictValue"-->
<!--                  :label="dict.dictValue"-->
<!--                >{{ dict.dictLabel }}-->
<!--                </el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="上传附件" prop="noticeFile">
              <FileUpload v-model="form.noticeFile"/>
            </el-form-item>
            <el-form-item label="污染源企业类型" >
              <el-select v-model="queryParams.epType" placeholder="请选择" @change="queryEp">
                <el-option
                  v-for="dict in typeOptions_wrlx"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label=" 企业选择">
<!--              <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>-->
              <el-checkbox  v-model="checked" @change="checkedAll">全选/全不选</el-checkbox>
              <el-tree
                :data="data"
                show-checkbox
                default-expand-all
                node-key="id"
                ref="vuetree"
                highlight-current
                @check-change="handleCheckChange"
                :props="defaultProps">
              </el-tree>
            </el-form-item>
            <el-form-item label="内容">
              <editor v-model="form.noticeContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addNotice, delNotice, getNotice, listNotice, updateNotice} from "@/api/system/notice";
import {listInfo} from "@/api/enterprise/info";
// listInfo
import {getToken} from "@/utils/auth";
import {listInfo_common} from "../../../api/enterprise/info";
import {listNotice_common} from "../../../api/system/notice";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      checked:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      fileList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [],
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 状态数据字典
      typeOptions: [],
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
        epType: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          {required: true, message: "公告标题不能为空", trigger: "blur"}
        ],
        noticeType: [
          {required: true, message: "公告类型不能为空", trigger: "change"}
        ]
      },
      typeOptions_wrlx:[],
    };
  },
  created() {

    this.getList();
    this.getDicts("wrlx").then(response => {
      this.typeOptions_wrlx = response.data;
    });
    this.getDicts("sys_notice_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_notice_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getInfo();
  },
  methods: {
    getInfo(){
      listInfo_common(this.queryParams).then(res=>{
        var list=res.rows
        var arr=[];
        list.map(function(item){
          var value={
            id: item.epId,
            label: item.epName,

          }
          arr.push(value)

        });
        this.data=arr;
      })
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice_common(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0",
        noticeFile:"",
        noticeEp:"",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "通知";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.noticeId != undefined) {
            updateNotice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },









    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    checkedAll(){
      if (this.checked) {
        //全选
        this.$refs.vuetree.setCheckedNodes(this.data);
      }else{
        //取消选中
        this.$refs.vuetree.setCheckedKeys([]);
      }
    },
    handleCheckChange(data, checked){
     var arr= this.$refs.vuetree.getCheckedNodes()
      // var arr= this.$refs.vuetree.getCheckedNodes();
      if(arr){
        var usrArr=[];
        for(var i=0;i<arr.length;i++){
          //usrArr.push(arr[i].label);
          usrArr.push(arr[i].id);
        }
        var id=usrArr.join(",");
        this.form.noticeEp=id;
        // this.form.
      //   this.userName=name;
      //   this.form.lvAcceptP=name;
      //   this.form.lvDealState=1;
      // }else{
      //   this.form.lvDealState=0;
      // }

      // console.log(data)
      // if(checked){
      //   // this.listQuery.menuId = data.id
      //   // this.$refs.tree.setCheckedKeys([data.id],true)
      // }else{
      //   // this.listQuery.menuId = null
      // }
      }
    },
    queryEp(e){
      this.queryParams.epType = e;
      listInfo_common(this.queryParams).then(res=>{
          console.log(res);
          var usrArr=[];
          if(res.rows.length>0){
            for(var i=0;i<res.rows.length;i++){
              usrArr.push(res.rows[i].epId)
            }
            var id=usrArr.join(",");
            this.form.noticeEp=id;
          }
      })
    }
  }
};
</script>
